function _ctRenderHtmlToPdf(htmlText, baseName) {
var goBtn = document.getElementById('html-to-pdf-go');
if (goBtn) { goBtn.disabled = true; goBtn.textContent = 'Generating PDF...'; }
loadScriptPromise('https://cdnjs.cloudflare.com/ajax/libs/html2pdf.js/0.10.2/html2pdf.bundle.min.js').then(function() {
var container = document.createElement('div');
container.style.cssText = 'padding:24px;font-family:-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,sans-serif;color:#212529;line-height:1.5;max-width:780px;';
container.innerHTML = htmlText;
html2pdf().set({
margin: 10,
filename: baseName + '.pdf',
image: { type: 'jpeg', quality: 0.95 },
html2canvas: { scale: 2, useCORS: true },
jsPDF: { unit: 'pt', format: 'a4', orientation: 'portrait' }
}).from(container).outputPdf('blob').then(function(blob) {
add_file_output(URL.createObjectURL(blob), baseName + '.pdf');
if (goBtn) { goBtn.disabled = false; goBtn.innerHTML = ' Convert to PDF'; }
}).catch(function(err) {
alert('Could not render this HTML as PDF: ' + (err && err.message || err));
if (goBtn) { goBtn.disabled = false; goBtn.innerHTML = ' Convert to PDF'; }
});
}).catch(function() {
alert('Could not load the PDF library.');
if (goBtn) { goBtn.disabled = false; goBtn.innerHTML = ' Convert to PDF'; }
});
}
// No-op on every keystroke; user clicks the Convert button to render.
function convert(input) { return ''; }
$(function() {
$('#html-to-pdf-go').off('click').on('click', function() {
var input = $('#box1').val();
if (!input || !input.trim()) {
alert('Paste some HTML in the box first.');
return;
}
_ctRenderHtmlToPdf(input, 'html-document');
});
});
var _loadedScripts = {};
function loadScriptPromise(url) {
if (_loadedScripts[url]) return _loadedScripts[url];
_loadedScripts[url] = new Promise(function (resolve, reject) {
var s = document.createElement('script');
s.src = url;
s.onload = resolve;
s.onerror = reject;
document.head.appendChild(s);
});
return _loadedScripts[url];
}
function replaceAll(find, replace, str) {
return str.replace(new RegExp(find, 'g'), replace);
}
function beautify(str) {
var result = '';
var length = str.length;
var i = 0;
var braceCountLeft = 0;
var braceCountRight = 0;
var withinQuotes = false;
while (i < length) {
var c = str[i];
if (c == '"' && (i == 0 || c[i - 1] != '\\')) {
// non-escaped quotes
withinQuotes = !withinQuotes;
}
if (!withinQuotes && (c == '}' || c == '{' || c == ',')) {
console.log('Start####' + result);
// look back and remove carriage returns and whitespace that are already there
var resultIndex = result.length - 1;
while (resultIndex >= 0 && (result[resultIndex] == ' ' || result[resultIndex] == '\r' || result[resultIndex] == '\n' || result[resultIndex] == '\t')) {
resultIndex = resultIndex - 1;
result = result.substr(0, resultIndex + 1);
console.log('char ' + result[resultIndex] + '-----' + result + 'zzz ' + result.length + ' ' + resultIndex);
}
if (c == '{') {
braceCountLeft++;
result += c + '\r' + GetTabs(braceCountLeft - braceCountRight);
} else if (c == '}') {
braceCountRight++;
// precede with carriage return
result += '\r' + GetTabs(braceCountLeft - braceCountRight) + c;
} else if (c == ',') {
result += c + '\r' + GetTabs(braceCountLeft - braceCountRight);
}
var nextChar = '';
// advance through whitespace and remove carriage returns that are already there
while (i < length && (str[i + 1] == ' ' || str[i + 1] == '\r' || str[i + 1] == '\n' || str[i + 1] == '\t')) {
i++;
}
} else {
result += str[i];
}
i++;
}
return result;
}
function GetTabs(count) {
var result = '';
for (var i = 0; i < count; i++) {
result += ' ';
}
return result;
}